;;; $DOOMDIR/config.el -*- lexical-binding: t; -*-

;; Place your private configuration here! Remember, you do not need to run 'doom
;; sync' after modifying this file!


;; Some functionality uses this to identify you, e.g. GPG configuration, email
;; clients, file templates and snippets.
(setq user-full-name "John Doe"
      user-mail-address "john@doe.com")

;; Doom exposes five (optional) variables for controlling fonts in Doom. Here
;; are the three important ones:
;;
;; + `doom-font'
;; + `doom-variable-pitch-font'
;; + `doom-big-font' -- used for `doom-big-font-mode'; use this for
;;   presentations or streaming.
;;
;; They all accept either a font-spec, font string ("Input Mono-12"), or xlfd
;; font string. You generally only need these two:
;; (setq doom-font (font-spec :family "monospace" :size 12 :weight 'semi-light)
;;       doom-variable-pitch-font (font-spec :family "sans" :size 13))

;; There are two ways to load a theme. Both assume the theme is installed and
;; available. You can either set `doom-theme' or manually load a theme with the
;; `load-theme' function. This is the default:
;;(doom-big-font-mode 1)
(setq doom-theme 'doom-opera)
;; If you use `org' and don't want your org files in the default location below,
;; change `org-directory'. It must be set before org loads! (setq org-directory "~/org/")
;; This determines the style of line numbers in effect. If set to `nil', line
;; numbers are disabled. For relative line numbers, set this to `relative'.
(setq display-line-numbers-type 1)
;;Here are some additional functions/macros that could help you configure Doom:
;; - `load!' for loading external *.el files relative to this one
;; - `use-package!' for configuring packages
;; - `after!' for running code after a package has loaded
;; - `add-load-path!' for adding directories to the `load-path', relative to
;;   this file. Emacs searches the `load-path' when you load packages with
;;   `require' or `use-package'.
;; - `map!' for binding new keys
;;
;; To get information about any of these functions/macros, move the cursor over
;; the highlighted symbol at press 'K' (non-evil users must press 'C-c c k').
;; This will open documentation for it, including demos of how they are used.
;;
;; You can also try 'gd' (or 'C-c c d') to jump to their definition and see how
;; they are implemented.
;;(setq server-socket-dir "~/.emacs.d/server")

(setq deft-directory "~/myorg")
(server-mode 1)
(require 'auto-save)
(auto-save-enable)

(setq auto-save-silent t)   ; quietly save
(global-aggressive-indent-mode 1)
;;(global-visual-line-mode)
;;(global-hide-mode-line-mode)
(require 'recite)
(setq org-roam-directory "~/myorg")
(setq deft-use-filename-as-title t)

(defvar todo-list nil)

(load-file "~/todoList")
(defun todo-append ()
  (interactive)
  (push (format "%s"
                (read-minibuffer "todo-append: "))
        todo-list))
(require 'dash)
(require 'f)
(require 's)

(defun random-todo ()
  (interactive)
  (progn
    (setq
     my-item
     (nth
      (random (length todo-list))
      todo-list))
    (setq
     todo-list
     (-remove-item
      my-item
      todo-list))
    (org-journal-new-entry nil)
    (insert my-item)))

(defun todo-ivy ()
  (interactive)
  (ivy-read "todos:" todo-list
            :require-match t
            :action
            (lambda (x)
              (progn
                (org-journal-new-entry nil)
                (insert x)
                (-remove-item
                 x
                 todo-list)))))

(defun todo-save-list ()
  (interactive)
  (f-write
   (format "(setq todo-list '(\"%s\"))"
           (s-join "\" \"" todo-list))
   'utf-8 "~/todoList"))
(add-hook 'kill-emacs-hook 'todo-save-list)
;; (setq todo-list '("2342" "223" "2"))
;; (use-package pyim
;;   :ensure nil
;;   :config
;;   ;; 激活 basedict 拼音词库
;;   ;;(use-package pyim-basedict
;;   ;;:ensure nil
;;   ;;:config (pyim-basedict-enable))

;;   ;; 五笔用户使用 wbdict 词库
;;   (use-package pyim-wbdict
;;     :ensure nil
;;     :config (pyim-wbdict-v86-single-enable))

;;   ;;(setq default-input-method 'wubi)

;;   ;; 我使用全拼
;;   (setq pyim-default-scheme 'wubi)
;;   ;; 设置 pyim 探针设置，这是 pyim 高级功能设置，可以实现 *无痛* 中英文切换 :-)
;;   ;; 我自己使用的中英文动态切换规则是：
;;   ;; 1. 光标只有在注释里面时，才可以输入中文。
;;   ;; 2. 光标前是汉字字符时，才能输入中文。
;;   ;; 3. 使用 M-j 快捷键，强制将光标前的拼音字符串转换为中文。
;;   (setq-default pyim-english-input-switch-functions
;; 		'(pyim-probe-dynamic-english
;; 		  pyim-probe-isearch-mode
;; 		  pyim-probe-program-mode
;; 		  pyim-probe-org-structure-template))

;;   (setq-default pyim-punctuation-half-width-functions
;; 		'(pyim-probe-punctuation-line-beginning
;; 		  pyim-probe-punctuation-after-punctuation))

;;   ;; 开启拼音搜索功能
;;   (pyim-isearch-mode 1)

;;   ;; 使用 pupup-el 来绘制选词框
;;   (setq pyim-page-tooltip 'posframe)

;;   ;; 选词框显示5个候选词
;;   (setq pyim-page-length 5)

;;   ;; 让 Emacs 启动时自动加载 pyim 词库
;;   (add-hook 'emacs-startup-hook
;; 	    #'(lambda () (pyim-restart-1 t)))
;;   :bind
;;   (("M-u" . pyim-convert-string-at-point) ;与 pyim-probe-dynamic-english 配合
;;    ("C-;" . pyim-delete-word-from-personal-buffer)))

(require 'key-chord)
(key-chord-mode 1)
(key-chord-define-global "fd"  'evil-escape)

(map! :leader
      :desc "search" "j" #'counsel-search
      (:prefix ("v" . "gtd")
       :desc "capture" "i" #'org-gtd-capture
       :desc "capture" "c" #'org-capture-finalize
       :desc "finalize" "f" #'org-gtd-clarify-finalize
       :desc "next" "v" #'org-gtd-show-all-next
       :desc "process" "p" #'org-gtd-process-inbox
       :desc "steps" "s" #'inss)
      (:prefix ("t" . "todo")
       :desc "capture" "i" #'todo-append
       :desc "random" "t" #'random-todo
       :desc "done" "d" #'todo-ivy
       ))
(add-to-list 'load-path "~/.emacs.d/site-lisp/emacs-application-framework" )
(require 'eaf)
(require 'eaf-browser)
;;(require 'eaf-file-manager)
;;(require 'eaf-org-previewer)
;;(browse-url-browser-function 'eaf-open-browser)
(defalias 'browse-web #'eaf-open-browser)
(setq eaf-browser-default-zoom 1.5);(eaf-bind-key scroll_up "C-n" eaf-pdf-viewer-keybinding)
;;(eaf-bind-key scroll_down "C-p" eaf-pdf-viewer-keybinding)
;;(eaf-bind-key take_photo "p" eaf-camera-keybinding)
;;(eaf-bind-key nil "M-q" eaf-browser-keybinding)) ;; unbind, see more in the Wiki
(require 'cnfonts)
(cnfonts-enable)

(defun org-insert-heading-and-edit ()
  (interactive)
  (progn
    (evil-insert-line 0)
    (org-insert-heading 0)
    (org-edit-headline)
    (evil-escape)))

(defun org-insert-subheading-and-edit ()
  (interactive)
  (progn
    (evil-append-line 0)
    (org-insert-subheading 0)
    (org-edit-headline)
    (evil-escape)))

(defun org-search-headline ()
  (interactive)
  (eaf-open-browser
   (format "https://www.baidu.com/s?word=%s"
           (url-encode-url
            (org-get-heading)))))
(defun org-search-headline2 ()
  (interactive)
  (eaf-open-browser
   (format "https://www.google.com/search?q=%s"
           (url-encode-url
            (org-get-heading)))))


;;file:///usr/share/doc/racket/search/index.html?q=pict
(defun geiser-search ()
  (interactive)
  (progn
    (eaf-open-browser-other-window
     (format "file:///usr/share/doc/racket/search/index.html?q=%s" (symbol-at-point)))
    (sleep-for 1)
    (eaf-py-proxy-clear_focus)))

(global-set-key (kbd "<f1>") 'org-insert-heading-and-edit)
(global-set-key (kbd "<f2>") 'org-insert-subheading-and-edit)
(global-set-key (kbd "<f3>") 'org-search-headline)
(global-set-key (kbd "<f5>") 'org-search-headline2)
(global-set-key (kbd "<f6>") 'counsel-search)
(global-set-key (kbd "<f7>") 'counsel-outline)
(global-set-key (kbd "<f8>") 'webjump)
(global-set-key (kbd "<f9>") 'geiser-search)

(setq browse-url-browser-function 'eaf-open-browser)
(defalias 'browse-web #'eaf-open-browser)

;;(require 'eaf-evil)

;; (define-key key-translation-map (kbd "SPC")
;;   (lambda (prompt)
;;     (if (derived-mode-p 'eaf-mode)
;;         (pcase eaf--buffer-app-name
;;           ("browser" (if  (string= (eaf-call-sync "call_function" eaf--buffer-id "is_focus") "True")
;;                          (kbd "SPC")
;;                        (kbd eaf-evil-leader-key)))
;;           ("pdf-viewer" (kbd eaf-evil-leader-key))
;;           ("image-viewer" (kbd eaf-evil-leader-key))
;;           (_  (kbd "SPC")))
;;       (kbd "SPC"))))

(require 'pyim)
(setq ivy-re-builders-alist '((t . pyim-ivy-cregexp)))
(require 'ivy-posframe)
;; display at `ivy-posframe-style'
(setq ivy-posframe-display-functions-alist '((t . ivy-posframe-display)))
;; (setq ivy-posframe-display-functions-alist '((t . ivy-posframe-display-at-frame-center)))
;; (setq ivy-posframe-display-functions-alist '((t . ivy-posframe-display-at-window-center)))
;; (setq ivy-posframe-display-functions-alist '((t . ivy-posframe-display-at-frame-bottom-left)))
;; (setq ivy-posframe-display-functions-alist '((t . ivy-posframe-display-at-window-bottom-left)))
;; (setq ivy-posframe-display-functions-alist '((t . ivy-posframe-display-at-frame-top-center)))
(ivy-posframe-mode 1)
(require 'smooth-scrolling)
(smooth-scrolling-mode 1)

(setq my-rest-hook nil)
(defun my-rest ()
  (interactive)
  (progn
    (setq org-timer-start-time
          (time-add (current-time) 1200))
    (org-timer-set-mode-line 'on)
    (run-with-timer
     1200 nil
     (lambda ()
       (run-hooks 'my-rest-hook)))))
(add-hook 'my-rest-hook (lambda ()
                          (if (y-or-n-p "take a rest and type y or stop")
                              (my-rest)
                            (org-timer-stop))))

(setq vterm-shell "/bin/zsh")

;;(setenv "DEEPIN_WINE_SCALE" "1")
;;(setenv "QT_AUTO_SCREEN_SCALE_FACTOR" "0")
;; Set EAF application scale factor.
;;(setenv "QT_SCALE_FACTOR" "1")
;; Force xwayland to ensure SWay works.
;;(setenv "QT_QPA_PLATFORM" "xcb")

(require 'dbus)
(defun eudic ()
  (interactive)
  (progn
    (save-excursion
      (backward-word)
      (set-mark (point))
      (forward-word)
      (kill-ring-save (region-beginning)
                      (region-end)))
    (dbus-call-method
     :session
     "org.kde.StatusNotifierItem-13444-1"
     "/StatusNotifierItem"
     "org.kde.StatusNotifierItem"
     "Activate"
     :int32 0
     :int32 0)))

(global-set-key (kbd "<f8>") 'eudic)

(toggle-frame-fullscreen)

(defun call-fcitx-method (method)
  (dbus-call-method
   :session
   "org.fcitx.Fcitx"
   "/inputmethod"
   "org.fcitx.Fcitx.InputMethod"
   method))

(add-hook 'post-command-hook
          (lambda ()
            (and (equal ?\s (char-before))
                 (equal "wubi" (call-fcitx-method "GetCurrentIM"))
                 (call-fcitx-method "InactivateIM"))))

(defun insert-link-to-journal ()
  (interactive)
  (let ((link (org-store-link nil)))
    (org-journal-new-entry nil)
    (insert link)))

;;(require 'eaf-markdown-previewer)
;;(require 'eaf-rss-reader)
